﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("tokenShopSubject")]
    public class TokenShopSubjectController : AdminController
    {
        private ITokenShopSubjectService _iTokenShopSubjectService;
        private IProductService _iProductService;
        private IMerchantService _iMerchantService;

        public TokenShopSubjectController(ITokenShopSubjectService iTokenShopSubjectService, IProductService iProductService, IMerchantService iMerchantService)
        {
            _iTokenShopSubjectService = iTokenShopSubjectService;
            _iProductService = iProductService;
            _iMerchantService = iMerchantService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(TokenShopSubjectQuery query)
        {
            var model = _iTokenShopSubjectService.GetTokenShopSubjects(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            token_shop_subject_dto_ex model = new token_shop_subject_dto_ex() { status = ETokenShopSubjectStatus.Normal };
            if (id > 0)
                _iTokenShopSubjectService.GetTokenShopSubject(id).ToCopyProperty(model);
            model.status_value = ((int)model.status).ToString();//ng-select 需要绑定字符串类型
            model.type_value = ((int)model.type).ToString();
            model.img_url = model.img_url ?? "";
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(token_shop_subject_dto_ex dto)
        {
            dto.status = (ETokenShopSubjectStatus)int.Parse(dto.status_value);
            dto.type = (ETokenShopSubjectType)int.Parse(dto.type_value);
            var file_url = Upload("tokenShopSubject");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;
            else
                return Json(ResultToJson.Json(1001, "请先上传图片", new { }), JsonRequestBehavior.AllowGet);
            var result = _iTokenShopSubjectService.AddOrUpdate(dto);
            return Json(ResultToJson.Json(result ? " 保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(token_shop_subject_dto_ex dto)
        {
            dto.status = (ETokenShopSubjectStatus)int.Parse(dto.status_value);
            dto.type = (ETokenShopSubjectType)int.Parse(dto.type_value);
            var file_url = Upload("tokenShopSubject");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;

            var result = _iTokenShopSubjectService.AddOrUpdate(dto);
            return Json(ResultToJson.Json(result? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateStatus(long id, int status) {
            var result = _iTokenShopSubjectService.UpdateTokenShopSubjectStatus(id, status);
            return Json(ResultToJson.Json(result ? " 修改成功" : "修改失败", result));
        }

        #region 专题商品操作
        [HttpGet]
        public JsonResult GetSubjectProducts(TokenShopSubjectProductQuery query) {
            var model = _iTokenShopSubjectService.GetSubjectProducts(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetProducts(GoodsQuery query) {
            query.sale_status = 0;
            query.audit_status = 3;
            var tokenMerchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            query.merchant_id = tokenMerchant.id;
            var model = _iProductService.Get(query);
            List<product_dto> products = new List<product_dto>();
            foreach (var row in model.rows) {
                if (!_iTokenShopSubjectService.SubjectProductExist(query.subject_id, row.id))
                    products.Add(row);
            }
            model.rows = products;
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult AddProducts(List<token_shop_subject_product_dto> product_dtos) {
            var result = _iTokenShopSubjectService.AddProducts(product_dtos);
            if (product_dtos.Count > 0) {
                var temp = _iTokenShopSubjectService.CountProducts(product_dtos[0].subject_id);
            }
            return Json(ResultToJson.Json(result ? "该专题添加商品成功" : "该专题添加商品失败" , result), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult DeleteProduct(long id) {
            var result = _iTokenShopSubjectService.DeleteProduct(id);
            return Json(ResultToJson.Json(result ? "该专题删除商品成功" : "该专题删除商品失败" , result), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult AlterSort(token_shop_subject_product_dto product_dto) {
            var result = _iTokenShopSubjectService.AlterSort(product_dto);
            return Json(ResultToJson.Json(result ? "修改商品排序成功" : "修改商品排序失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}